package com.easyidea.its.domain.repository.user.impl;
/**
 *@author:
 *@version:
 *@description:
 */
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.easyidea.its.domain.model.system.User;
import com.easyidea.its.domain.repository.base.impl.BaseRepositoryImpl;
import com.easyidea.its.domain.repository.user.UserRepository;
import com.easyidea.its.util.ParamsUtilities;
import com.easyidea.its.util.StringUtil;
import com.easyidea.its.web.dto.UserDto;

public class UserRepositoryImpl extends BaseRepositoryImpl<User> implements
		UserRepository {

	public int getCountOfUser(String userName, String name) {
		// TODO Auto-generated method stub
		return 0;
	}

	public Collection<User> getUsers(String name, String userName, int startAtIndex, int count) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean hasUser(Integer id, String userName) {
		// TODO Auto-generated method stub
		return false;
	}

	public User loginCheck(String userName, String password) {
		Criteria criteria = createUndeleteCriteria();
		if(StringUtil.isNullOrBlank(userName)||StringUtil.isNullOrBlank(password)){
			return null;
		}
		criteria.add( Restrictions.eq("userName", userName.trim()));
		criteria.add( Restrictions.eq("password", password.trim()));
		return  (User)criteria.uniqueResult();
	}
	
	public int getUserCountByUserDto(UserDto userDto) {
		Criteria ca = getCriteriaByUserDtoForServicesManager(userDto);
		ca.setProjection(Projections.countDistinct("id"));
		return (Integer) ca.uniqueResult();
	}

	private Criteria getCriteriaByUserDtoForServicesManager(UserDto userDto) {
		Criteria ca = this.createCriteria();
		if (userDto != null) {
			if (userDto.getUserName() != null
					&& !userDto.getUserName().equals("")) {
				ca.add(Restrictions.like("userName", ParamsUtilities
						.dealWithReservedSqlChar(userDto.getUserName()),
						MatchMode.START));
			}

			if (userDto.getDeptId() != null && userDto.getDeptId() != -1) {
				ca.add(Restrictions.eq("department.id", userDto.getDeptId()));
			}
			if (userDto.getStatus() != null && userDto.getStatus() != -1) {
				ca.add(Restrictions.eq("status", userDto.getStatus()));
			}
			if (userDto.getName() != null && !userDto.getName().equals("")) {
				ca.add(Restrictions.like("name", ParamsUtilities
						.dealWithReservedSqlChar(userDto.getName()),
						MatchMode.START));
			}
//			if (userDto.getUserType() != null) {
//				ca.add(Restrictions.eq("userType", userDto.getUserType()));
//			}
//			if (userDto.getCreateTime() != null
//					&& !userDto.getCreateTime().equals("")) {
//				Date registDate = DateUtil.strToDate(userDto.getCreateTime());
//				ca.add(Restrictions.between("createDate", DateUtil
//						.getStartTime(registDate), DateUtil
//						.getEndTime(registDate)));
//			}
		}

		return ca;
	}

	public Collection<User> getUsersByUserDto(UserDto userDto, int start,
			int pageSize) {
		Criteria ca = getCriteriaByUserDtoForServicesManager(userDto);

		if (start > 0) {
			ca.setFirstResult(start);
		}
		if (pageSize > 0) {
			ca.setMaxResults(pageSize);
		}
		ca.addOrder(Order.desc("id"));
		return (Collection<User>) ca.list();
	}

}
